package com.csy.tcshop.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

//import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.csy.tcshop.product.entity.AttrEntity;
import com.csy.tcshop.product.entity.vo.AttrGroupRelationVo;
import com.csy.tcshop.product.entity.vo.AttrGroupsWithAttrsVo;
import com.csy.tcshop.product.service.AttrAttrgroupRelationService;
import com.csy.tcshop.product.service.AttrService;
import com.csy.tcshop.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.csy.tcshop.product.entity.AttrGroupEntity;
import com.csy.tcshop.product.service.AttrGroupService;
import com.csy.common.utils.PageUtils;
import com.csy.common.utils.R;


/**
 * 属性分组
 *
 * @author 陈盛英
 * @email 1404630967@qq.com
 * @date 2020-12-10 00:25:45
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    private CategoryService categoryService;

    @Autowired
    AttrService attrService;

    @Autowired
    AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 获取当前分类下的所有属性分组，极其分组下的所有属性
     *
     * @return
     */
    //    product/attrgroup/225/withattr
    @GetMapping("/{catelogId}/withattr")
    public R getCategoryGroupWithAttrs(@PathVariable("catelogId") Long catelogId) {
        List<AttrGroupsWithAttrsVo> list = attrGroupService.getCategoryGroupWithAttrs(catelogId);
        return R.ok().put("data", list);
    }

    /**
     * 新增关联关系的功能
     */
    @PostMapping("/attr/relation")
    public R addAttrRelation(@RequestBody List<AttrGroupRelationVo> vos) {
        attrAttrgroupRelationService.saveBatch(vos);
        return R.ok();
    }

    /**
     * 查找与属性分组的没有关联规格参数
     *
     * @param attrgroupId
     * @return
     */
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@RequestParam Map<String, Object> params,
                            @PathVariable("attrgroupId") Long attrgroupId) {
        PageUtils pageUtils = attrService.getNoRelationAttrPage(params, attrgroupId);
        return R.ok().put("page", pageUtils);
    }

    /**
     * 删除属性分组的关联规格参数
     */
    @PostMapping("/attr/relation/delete")
    //@RequiresPermissions("product:attr:delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVo[] vos) {
        attrService.deleteRelation(vos);
        return R.ok();
    }

    /**
     * 查找与属性分组的关联规格参数
     *
     * @param attrgroupId
     * @return
     */
    @GetMapping("/{attrgroupId}/attr/relation")
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId) {
        List<AttrEntity> entities = attrService.getRelationAttr(attrgroupId);
        return R.ok().put("data", entities);
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("product:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable("catelogId") Long catelogId) {
        PageUtils page = attrGroupService.queryPage(params, catelogId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("product:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        Long[] path = categoryService.findCategoryPath(attrGroup.getCatelogId());
        attrGroup.setCatelogPath(path);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
